package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior>
        implements ApLikesBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    /**
     * 保存点赞行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //参数校验
        if (dto==null||dto.getArticleId()==null
        ||dto.getOperation().intValue()<0||dto.getOperation().intValue()>1
        ||dto.getType().intValue()<0||dto.getType().intValue()>2){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //查询实体类
        ApBehaviorEntry behavior = apBehaviorEntryService.
                findByUserIdOrEquipmentId(user.getId(), null);
        if (behavior==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //查询点赞的行为是否已存在
        ApLikesBehavior likesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, behavior.getId()));
        //不存在 判断是否是点赞行为
        if (likesBehavior==null&&ApLikesBehavior.Operation.CANCEL.equals(dto.getOperation())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"暂无点赞，无需取消");
        }
        if (likesBehavior!=null &&
                likesBehavior.getOperation().equals(ApLikesBehavior.Operation.LIKE)&&
                ApLikesBehavior.Operation.LIKE.getCode()==dto.getOperation().shortValue()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,
                    "已点赞，无需重复点赞");
        }
        //保存点赞操作
        if (likesBehavior==null&&ApLikesBehavior.Operation.LIKE.getCode()==dto.getOperation().shortValue()){
            likesBehavior = new ApLikesBehavior();
            likesBehavior.setOperation(dto.getOperation());
            likesBehavior.setCreatedTime(new Date());
            likesBehavior.setType(dto.getType());
            likesBehavior.setArticleId(dto.getArticleId());
            likesBehavior.setEntryId(behavior.getId());
            save(likesBehavior);
        }else { //或者是取消点赞
            likesBehavior.setOperation(ApLikesBehavior.Operation.CANCEL.getCode());
            updateById(likesBehavior);
        }


        //返回结果

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
